package com.lmxdawn.api.dnf.dto;

import com.wechat.pay.java.core.cipher.RSASigner;
import com.wechat.pay.java.core.cipher.SignatureResult;
import com.wechat.pay.java.core.cipher.Signer;
import com.wechat.pay.java.core.util.IOUtil;
import com.wechat.pay.java.core.util.PemUtil;
import com.wechat.pay.java.core.util.StringUtil;
import lombok.Data;

import java.io.IOException;
import java.security.PrivateKey;

@Data
public class WxJsapiPrepayResp {

    private String appId;

    private String timestamp;

    private String nonceStr;

    private String packageValue;

    private String signType;

    private String sign ;


    public String generateSign(String serialNumber, String privateKeyPath) throws IOException {
        String sb = StringUtil.toIndentedString(appId) + "\n" +
                StringUtil.toIndentedString(timestamp) + "\n" +
                StringUtil.toIndentedString(nonceStr) + "\n" +
                StringUtil.toIndentedString(packageValue) + "\n";
        String privateKeyStr = IOUtil.loadStringFromPath(privateKeyPath);
        PrivateKey key = PemUtil.loadPrivateKeyFromString(privateKeyStr);
        Signer rsaSigner = new RSASigner(serialNumber, key);
        SignatureResult signatureResult = rsaSigner.sign(sb);
        return signatureResult.getSign();
    }
}
